package cn.itcast.bos.web.action.base;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@ParentPackage("json-default")
@Namespace("/")
@Action
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {

	private  Standard standard = new Standard();
	
	@Autowired
	private StandardService standardService;
	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="/pages/base/standard.html")})
	public String save(){
		standardService.save(standard);
		return SUCCESS;
	}
	private int page;
	private int rows;
	public void setRows(int rows) {
		this.rows = rows;
	}
	public void setPage(int page) {
		this.page = page;
	}
	//获取页面自动传过来的page和size的值
	
	
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		System.out.println("page"+page+"rows"+rows);
		Pageable pageable=new PageRequest(page-1, rows);
		Page<Standard> page=standardService.findPageData(pageable);
		//返回页面需要的total和rows数据 row表示每夜的数据 total表示总的记录数
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		//将map转成json 使用struts2中的json 直接把数据压入valuestack栈顶就行了
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		//直接调用standardService的方法
		List<Standard> list=standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
